function modeStruct = compute_mode_diff(data,lowThresh,binsize,numbins,sensitivity,smoother,smooth_value,plotflag)
%specific to FCS3.0 files
% binsize=2^8;
% % lowThresh==10^1;
% % sensitivity=0.5;
% %specific to FCS2.0 files
% binsize=2^6;
% lowThresh=10^0.2;
% %startValue=10^0.2;
% sensitivity=100;

%plotflag=0;
%generate logrithmic bins
scaled_bins=exp((1:binsize)*(log(numbins)/binsize));

[x nX]=hist(data,scaled_bins);
y=smooth(x,smooth_value,smoother);
z=diff(y);
zS=smooth(z,smooth_value,smoother);
zS(end+1)=0;

%Determines the index of the values is above the low value threshold.
%Essentially cuts off initial bins
startValue=find(min(nX(nX>lowThresh))==nX);
%nX
if(plotflag)
    figure
    bar(nX,zS);
    hold on
    set(get(gcf,'CurrentAxes'),'XScale','Log');
end
%mode.value=NaN;
%title(parName);

%make sure the initial slope is positive.  Initial negative sloped peaks
%are either noise or a crud peak which has collected near the zero peak
positive_value=1;
sign_prev=sign(positive_value);

cluster=0;
ind=zeros(length(zS),2);

for i=startValue:length(zS)
    ind(i,1)=zS(i);
    
    if(abs(zS(i))>sensitivity)
        %If the cluster is 0 and the sign is negative it is a
        %peak to be discarded (see above)
        if(cluster==0)
            if(sign(zS(i))==sign(-1))
                ind(i,2)=0;
            else
                %sign must be positive, begin initial peak
                sign_prev=sign(zS(i));
                cluster=cluster+1;
                ind(i,2)=cluster;
            end
        elseif(sign_prev==sign(zS(i)))
            ind(i,2)=cluster;
        else
            sign_prev=sign(zS(i));
            cluster=cluster+1;
            ind(i,2)=cluster;
        end
    else
        ind(i,2)=0;
    end
    
end
%ind
for j=1:cluster
    lower=min(nX(find(ind(:,2)==j)));
    upper=max(nX(find(ind(:,2)==j)));
    if(plotflag)
        line([lower lower],[0 max(zS)*1.05],'Color','g');
        line([upper upper],[0 max(zS)*1.05],'Color','r');
    end
end

for k=1:cluster/2
    
    modeStruct(k).type='diff';
    modeStruct(k).binsize=binsize;
    modeStruct(k).sensitivity=sensitivity;
    modeStruct(k).smoother=smoother;
    modeStruct(k).smooth_value=smooth_value;
    
    
    mLower=min(nX(find(ind(:,2)==(2*k-1))));
    mUpper=max(nX(find(ind(:,2)==(2*k))));
    mode_subset=data(data >mLower & data < mUpper);
    %length(mode_subset)
    %binsize=2^8;
    %Reduce the bin size one half the original order of magnitude
    binsize=fix(exp(log(length(mode_subset))/2));
    [v nV]=hist(mode_subset,binsize);
    
    %w=smooth(v,smooth_value,smoother);
    %log10(length(mode_subset))
    w=smooth(v,5,smoother);
    if(plotflag)
        figure
        bar(nV,w)
        title(['mode' num2str(k)]);
    end
    %length(w)
    % max(w)
%     w
%     find(w==max(w),1,'first')
%     w(find(w==max(w),1,'first'))
    %modeStruct(k).value=nV(find(w==max(w)));
    modeStruct(k).value=nV(find(w==max(w),1,'first'));
    %modeStruct(k).value=NaN;
    modeStruct(k).lower=mLower;
    modeStruct(k).upper=mUpper;
    modeStruct(k).counts=length(mode_subset);
    modeStruct(k).percentage=modeStruct(k).counts/length(data);
    modeStruct(k).std=std(mode_subset);
end

% for i=1:length(modeStruct)
%     modeStruct(i)
% end